/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Hacker
 */
@WebServlet(name="mostrarproductos", urlPatterns={"/mostrarproductos"})
public class mostrarproductos extends HttpServlet {
   Connection c;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

           String fil=request.getParameter("buscar");

            HttpSession sesion=request.getSession(false);

            if(sesion== null){
            response.sendRedirect("login");
            }else{


      if (fil==null)
          fil="";
      Class.forName("org.gjt.mm.mysql.Driver");
                 c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/tablas", "root", "root");
            Statement sql = (Statement) c.createStatement();

              ResultSet datos = sql.executeQuery("select * from productos where nombre like '%"+fil+"%' ");



            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Registrarse</title>");
            out.println("</head>");
            out.println("<body background='Imagenes/i love motivos.JPG' link='#FF0000' vlink='#FF0000' alink='#FF0000'>");

//                out.println("<table border ='2'width='400' align='center'>");
//                out.println(" <tr style='color:#FF0000'>" +
//
//                    "</tr>");
//               out.println("<br>");




             




//
//                out.println("</table>");
              out.println("<center>");
              out.println("<form id='form1' name='filtro' method='get' action='mostrarproductos'>");
              out.println(" <p><label>Producto: <input type='text' name='buscar' id='textfield1'/> <input type='submit' name='button' id='button' value='buscar' /> </label> </p>");
              out.println("</form>");
              out.println("</center>");



            out.println("<table bgcolor='#FFFFFF' border ='2'width='400' align='center'>");
            out.println(" <tr style='color:'#FFFFFF'>" +
                    "<th bgcolor='#FF0000' scope='col'>id</th>" +
                    "<th bgcolor='#FF0000' scope='col'>nombre</th> " +
                    "<th bgcolor='#FF0000' scope='col'>marca</th>" +
                    "<th bgcolor='#FF0000' scope='col'>precio</th> " +
                    "<th bgcolor='#000000' scope='col'><a href='insertarproducto'>agregar</a></th> " +
                    "</tr>");


      while(datos.next()){
           out.println("<tr align='center'><td>"+datos.getString("id")+"" +
                   "" +
                   "</td bgcolor=''><td>"+datos.getString("nombre")+"</td>" +
                   "<td>"+datos.getString("marca")+"</td>" +
                   "<td>"+datos.getString("precio")+"</td>" +
                  // "<td>"+datos.getString("precio")+"</td>" +
                   "<td><input type='button' value='Eliminar' onclick= \"if(confirm('¿Estas seguro que quieres eliminar?')){location.href='eliminarproducto?ids="+datos.getString("id")+"'} \"/>"+
                   "<td><a href='modificarproducto?idm="+datos.getString("id")+"'>Modificar</a></td>" +
                  "<td><a href='insertarproducto'>agregar</a></td>" +
                   "</tr>");
                       }
 out.println("<a href='Mostrar'>Usuarios</a>");
 out.println("<br>");
 out.println("<br/>");
 out.println("<br/>");
// out.println("<center>");
 out.println("<MARQUEE><img src='./Imagenes/031027_love_espagne.gif'/></MARQUEE>");
 out.println("<br/>");
 out.println("<a href='login'>login</a>");
 out.println("<br>");
 out.println("<br/>");
 out.println("<br/>");
 out.println("<img src='./Imagenes/ani3dpre.gif'/>");
 out.println("<br>");
 out.println("<br/>");
 out.println("<br/>");
  out.println("<a href='perfiles'>Ver mi Perfil</a>");
           out.println("</table>");

            out.println("</body>");
            out.println("</html>");
        }} finally {
            out.close();
        }
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(mostrarproductos.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(mostrarusuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(mostrarproductos.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(mostrarusuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
